#! /usr/bin/env python
import pygame
import basicSprite
import spellSprite
from helpers import *
from textgameprog import *

class Wizzard(basicSprite.Sprite):
    """This is our Wizzard that will move around the screen
    and cast spells"""
    
    def __init__(self, centerPoint, image, color):
        self.health = 100
        self.color = color
        self.centerPoint = centerPoint
        basicSprite.Sprite.__init__(self, centerPoint, image)
        """Set the number of Pixels to move each time"""
        self.x_dist = 4
        self.y_dist = 4 
        """Initialize how much we are moving"""
        self.xMove = 0
        self.yMove = 0
        self.hit = ""

        """Prevent inadvertent movement due to keys held down when
        the wizzard gains focus"""
        self.canMoveLeft = False
        self.canMoveRight = False
        self.canMoveUp = False
        self.canMoveDown = False

    def MoveKeyDown(self, key):
        """This function handles KEYDOWN input events when the Wizzard
            is the focus. If the key is a directional key, it sets the
            xMove or yMove variables that will then move the Wizzard
            when update() function is called.  The xMove and yMove
            values will be returned to normal when the key's MoveKeyUp
            function is called."""
        if (key == K_RIGHT):
            self.canMoveRight = True
            self.xMove += self.x_dist
        elif (key == K_LEFT):
            self.canMoveLeft = True
            self.xMove += -self.x_dist
        elif (key == K_UP):
            self.canMoveUp = True
            self.yMove += -self.y_dist
        elif (key == K_DOWN):
            self.canMoveDown = True
            self.yMove += self.y_dist
        
    def MoveKeyUp(self, key):
        """This function handles KEYUP input events when the Wizzard
            is the focus. If the key is a directional key, it resets
            the xMove or yMove variables that will then move the
            Wizzard when update() function is called.  The xMove and
            yMove values will be returned to normal when this key's
            MoveKeyUp function is called."""
        if (key == K_SPACE):
            self.stopMoving()
            pygame.event.post(pygame.event.Event(FOCUS_CHANGE, focus="nextWizzard"))
        elif (key == K_RETURN):
            self.stopMoving()
            pygame.event.post(pygame.event.Event(FOCUS_CHANGE, focus="menu"))
        elif (key == K_RIGHT):
            if self.canMoveRight:
                self.xMove += -self.x_dist
        elif (key == K_LEFT):
            if self.canMoveLeft:
                self.xMove += self.x_dist
        elif (key == K_UP):
            if self.canMoveUp:
                self.yMove += self.y_dist
        elif (key == K_DOWN):
            if self.canMoveDown:
                self.yMove += -self.y_dist

    def stopMoving(self):
        if self.xMove != 0:
            self.xMove = 0
        if self.yMove != 0:
            self.yMove = 0
        self.canMoveLeft = False
        self.canMoveRight = False
        self.canMoveUp = False
        self.canMoveDown = False
            
    def update(self):
        # returns true if moved
        """Called when the Wizzardsprite should update itself"""
        
        if (self.xMove==0)and(self.yMove==0):
            """If we arn't moveing just get out of here"""
            return False
        """All right we must be moving!"""
        self.rect.move_ip(self.xMove,self.yMove)
        return True 

    def cast(self, coscoeff, polycoeffs, typeof):
        if typeof == "cos":
            type = 0
        elif typeof == "poly":
            type = 1
        else:
            type = 2
        newSpell = spellSprite.Spell(self.rect.center, None, self, coscoeff, polycoeffs, type)
        newSpell.calculate_spell_positions()
        pygame.event.post(pygame.event.Event(SPELL_CAST, spell=newSpell))
        
#        if pygame.sprite.spritecollideany(self, block_group):
#            """IF we hit a block, don't move - reverse the movement"""
#            self.rect.move_ip(-self.xMove,-self.yMove)
        
#        """Check to see if we hit a spell!"""
#        lst_spells = pygame.sprite.spritecollide(self, spell_group, False)
#        if (len(lst_spells)>0):
#            """All right we have hit a spell!"""
#            self.SpellCollide(lst_spells)
                
    def SpellCollide(self, lstSpells):
        """This Function is called when the Wizzard collides with a Spell
        lstSpells is a list of Spell sprites that it has hit."""
        
        if (len(lstSpells)<=0):
            """If the list is empty, just get out of here"""
            return
    
        """Loop through the spellsand see what should happen"""
        for spell in lstSpells:
            """Looks like we're hit"""
            pygame.event.post(pygame.event.Event(WIZZARD, status="damaged"))
                
    def damage(self):
        if (self.health - 10 <= 0):
            pygame.event.post(pygame.event.Event(WIZZARD, status="dead"))
            self.hit = "OHNOES!"
        self.health -= 10        
        
